package org.javaboy.quartz_demo.controller;

import org.javaboy.quartz_demo.model.QuartzBean;
import org.javaboy.quartz_demo.model.RespBean;
import org.javaboy.quartz_demo.service.QuartzJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定时任务接口，在这个接口中处理定时任务的增删改查操作
 */
@RestController
@RequestMapping("/job")
public class QuartzJobController {
    @Autowired
    QuartzJobService quartzJobService;

    @GetMapping
    public List<QuartzBean> getAll() {
        return quartzJobService.getAllJobs();
    }

    @PostMapping("runOnce")
    public RespBean runOnce(String jobName, String jobGroup) {
        return quartzJobService.runOnce(jobName, jobGroup);
    }

    @PostMapping
    public RespBean addJob(@RequestBody QuartzBean quartzBean) {
        return quartzJobService.addJob(quartzBean);
    }

    @PutMapping
    public RespBean updateJob(@RequestBody QuartzBean quartzBean) {
        return quartzJobService.updateJob(quartzBean);
    }

    /**
     * 更新触发器
     * @param quartzBean
     * @return
     */
    @PutMapping("/trigger")
    public RespBean updateTrigger(@RequestBody QuartzBean quartzBean) {
        return quartzJobService.updateTrigger(quartzBean);
    }

    /**
     * 删除任务
     * @param jobName
     * @param jobGroup
     * @return
     */
    @DeleteMapping
    public RespBean deleteJob(String jobName, String jobGroup) {
        return quartzJobService.deleteJob(jobName, jobGroup);
    }

    /**
     * 切换定时任务状态
     * 如果现在是运行状态，则暂停定时任务
     * 如果是暂停状态，则恢复定时任务
     * @param quartzBean
     * @return
     */
    @PostMapping("/changeStatus")
    public RespBean changeStatus(@RequestBody QuartzBean quartzBean) {
        return quartzJobService.changeStatus(quartzBean);
    }

}
